﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Xml;
using System.ComponentModel.Design;
using System.Drawing;
using UmcControls.DesignInterface;
using UmcProvider.Shared;
using System.Text.RegularExpressions;
using Wicresoft.Shell.Core;

namespace Wicresoft.Shell.Package.AppExp.ToolboxControl
{
    //TODO : UmcBody should implement some other properties , nowadays , it only support size and back color
    [ToolboxItem(false)]
    public class UmcBody : UserControl, IConverter, IUmcPropertyStore
    {
        //store the properties parse from code which is not surpported by designer
        //also store the properties which is set value using script variable
        //users should operator the dic through the IUmcPropertyStore
        private Dictionary<string, string> DesignViewUnSurpportProperty = new Dictionary<string, string>();

        #region IUmcPropertyStore Members

        public void AddUmcProperty(string property, string value)
        {
            DesignViewUnSurpportProperty.Add(property, value);
        }

        public void RemoveUmcProperty(string property)
        {
            DesignViewUnSurpportProperty.Remove(property);
        }

        public void RemoveAllUmcProperties()
        {
            DesignViewUnSurpportProperty.Clear();
        }

        public string GetUmcProperty(string property)
        {
            if (DesignViewUnSurpportProperty.ContainsKey(property))
            {
                return DesignViewUnSurpportProperty[property];
            }
            else
            {
                return null;
            }
        }

        public List<KeyValuePair<string, string>> GetUmcPropertyList()
        {
            return DesignViewUnSurpportProperty.ToList();
        }


        #endregion

        #region IConverter Members

        public XmlNode[] Serialize(IComponent root,XmlDocument document)
        {
            XmlNode node = document.CreateNode(XmlNodeType.Element, UmcConstant.UmcBodyName, null);
            /*now design only support size*/
            IUmcPropertyStore ips = (root as IUmcPropertyStore);
            //for size
            string p0property = ips.GetUmcProperty("Size");
            if (p0property == null)
            {
                node.Attributes.Append(document.CreateAttribute("Size"))
                    .Value = UmcPropertyConvert.ToString(this.Size);
            }
            else
            {
                node.Attributes.Append(document.CreateAttribute("Size"))
                    .Value = p0property;
            }
            ////for back color
            //p0property = ips.GetUmcProperty("BackColor");
            //if (p0property == null)
            //{
            //     node.Attributes.Append(document.CreateAttribute("BackColor"))
            //        .Value = string.Format("{0},{1},{2}", this.BackColor.R, this.BackColor.G, this.BackColor.B);
            //}
            //else
            //{
            //    node.Attributes.Append(document.CreateAttribute("BackColor"))
            //        .Value = p0property;
            //}
            ////other stored properties
            foreach (KeyValuePair<string, string> k in ips.GetUmcPropertyList())
            {
                node.Attributes.Append(document.CreateAttribute(k.Key))
                   .Value = k.Value;
            }


            return new XmlNode[] { node };
        }

        public IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            //document.DocumentElement.OuterXml;
            //Script component = new Script();
            //component.Tag = node.First().OuterXml;
            //return component;
            foreach (XmlAttribute attr in node.First().Attributes)
            {                    
                string variablePattern = @"\$\w+";
                if (attr.Name == "Size")
                {
                    MatchCollection mc = Regex.Matches(attr.Value, variablePattern);
                    foreach (Match m in mc)
                    {
                        UmcVariable uvble;
                        if (IntelliSenseManager.FullcontextVariable.TryGetValue(m.Value.Substring(1, m.Value.Length - 1), out uvble))
                            attr.Value = attr.Value.Replace(m.Value, uvble.VariableValue);
                    }
                    try
                    {
                        var source = attr.Value.Split(',');
                        this.Size = new Size(int.Parse(source[0]), int.Parse(source[1]));
                    }
                    catch
                    {
                        this.Size = new Size(200, 323);
                        (this as IUmcPropertyStore).AddUmcProperty("Size", attr.Value);
                    }
                }
                //else if (attr.Name == "BackColor")
                //{
                //    try
                //    {
                //        var source = attr.Value.Split(',');
                //        this.BackColor = Color.FromArgb(int.Parse(source[0]), int.Parse(source[1]), int.Parse(source[2]));
                //    }
                //    catch
                //    {
                //        this.BackColor = Color.FromKnownColor(KnownColor.Control);
                //        (this as IUmcPropertyStore).AddUmcProperty("BackColor", attr.Value);
                //    }
                //}
                else
                {
                    (this as IUmcPropertyStore).AddUmcProperty(attr.Name, attr.Value);
                }
            }
            //store the whole document
            this.Tag = document.DocumentElement.OuterXml;

            return this;
        }

        #endregion

    }
}
